<?php

/**
 * PlaceController
 * @author ZloY aka Maxim Konovalov max.zloy@gmail.com
 */
class PlaceController extends Zend_Controller_Action {
	public function indexAction() {
		//@todo убрать костыль
		$this->_redirect('category');
	}
	
	public function viewAction()
	{
		$id = (int) $this->_getParam('id',0);
		$place = new Model_Place($id);
		$categorys = $place->getCategorys();
		$this->view->categorys =  $categorys;
		$this->view->currentCategory =  $categorys[0]; 
		$currentCategory  = new Model_Category_Place((int) $categorys[0]['id']);
		$this->view->parent = $currentCategory->getParent();
		$this->view->place = $place->getRow();
		$this->view->album = new  ZloY_Album_Place($id);
		$this->view->commentForm = new Form_Comment($id);
		$this->view->childrens = $place->getChildren();
		$comments = new Model_Comments(null, 'place');
		$this->view->comments = $comments->getComments($id);
		$was = new Model_Rating(null, 'was');
		$this->view->isWas = $was->isVoted($id);
	}
	
	public function addAction()
	{
		if(!Zend_Auth::getInstance()->hasIdentity()){
			$this->_redirect('/user/login');
		}
		$category_id = (int) $this->_getParam('category_id', null);
		$id = (int) $this->_getParam('id', 0);
		$this->view->id = $id;
		$form = $this->view->form = new Form_Place($category_id);
		$categoryElement = $form->getElement('child_category1');
		$category = new Model_Category_Place();
		foreach ($category->getAll() as $category){
			$categoryElement->addMultiOption($category->id, $category->name);
		}
		
		if($id>0){
			$place = new Model_Place($id);
			$categorys = $this->view->categorys = $place->getCategorys();
			$album = new ZloY_Album_Place($id);
			$category = new Model_Category_Place();
			$this->view->parents = $category->getLevel(0);
		} else {
			$place = new Model_Place();
		}
		$form->populate($place->getRow()->toArray());
		$this->view->place = $place;
		if($this->getRequest()->isPost()){
			if($form->isValid($this->getRequest()->getPost())){
				 $data  = $this->getRequest()->getPost();
				 $data['created'] = new Zend_Db_Expr('NOW()');
				 $data['publicate']  = 1;
				 $place->fill($data);
				 $connect = new Model_Connect_Category_Place();
				 $i = 1;
				 $connect->delete('item_id = ' . $place->getRow()->id);
				 foreach($_POST as $key=>$value)
				 {
				 	$arr = explode('category', $key);
				 	if(count($arr)>1 && $arr[0] != 'parent_'){
				 		
				 		$connect->add($data['child_category'.$i], $place->getRow()->id);
				 		$i++;
				 	}
				 }
				 $place->save();
				 $this->_redirect('/place/view/id/'.$place->getRow()->id);
			}
		}
	}
	
	
	public function saveAction()
	{ 
		Zend_Layout::getMvcInstance()->disableLayout();
		$id =  $this->_getParam('id',null);
		$place = new Model_Place($id);
		
		$data  = $this->getRequest()->getPost();
		
		if(!$id){
			$data['created'] = new Zend_Db_Expr('NOW()');
			$data['modifided'] =  $data['created'];
		} else {
			$data['modifided'] = new Zend_Db_Expr('NOW()');	
		}
		$data['user_id'] = Zend_Auth::getInstance()->getIdentity()->id;
		$place->fill($data);
		$row = $place->save();
		
		echo $row->id;

	}
	
	public function editAction()
	{
		if(!Zend_Auth::getInstance()->hasIdentity()){
			$this->_redirect('/user/login');
		}
		$id = (int) $this->_getParam('id', 0);
		$this->view->id = $id;
		$form = new Form_Place();
		$form->setAction('/place/add/id/' . $id);
		$place = new Model_Place($id);
		$this->view->place = $place->getRow();
		$form->populate($place->getRow()->toArray());
		$this->view->categorys = $place->getCategorys();
		$category = new Model_Category_Place();
		$this->view->parents = $category->getLevel(0);
		$this->view->form = $form;
	}
	public function deleteAction()
	{
		
	}
	
	public function getgalleryAction()
	{
		Zend_Layout::getMvcInstance()->disableLayout();
		$id = (int) $this->_getParam('id',0);
		$album = new ZloY_Album_Place($id);
		$this->view->images = $album->getImages('s');
	}
	
	public function getcategoryAction()
	{
		Zend_Layout::getMvcInstance()->disableLayout();
		$id = (int) $this->_getParam('id',0);
		$arr = explode('category', $_POST['name']);
		$form = new Form_Place($id);
		$this->view->element = $form->getElement('child_category1')->setName('child_category' . $arr[1])->clearValidators(); 
	}
	
	public function deleteimageAction()
	{
		Zend_Layout::getMvcInstance()->disableLayout();
		$place = new Model_Place();
		$max_id = (int) $place->getMaxId(0);
		$album = new ZloY_Album_Place($max_id);
		$album->delete($this->_getParam('src'));
		$this->view->images = $album->getImages('s');
	}
	
	public function wasAction()
	{
		$place = new Model_Place((int) $this->_getParam('item_id',0));
		$place->setRatingClass(new Model_Rating(null, 'was'));
		$place->setWas(1);
		if($this->getRequest()->isXmlHttpRequest()){
			Zend_Layout::getMvcInstance()->disableLayout();
		} else {
			$this->_redirect($_SERVER['HTTP_REFERER']);
		}
	}
	
	public function parentAction()
	{
		$place = new Model_Place((int) $this->_getParam('id',0));
		if ($this->getRequest()->isXmlHttpRequest()) {
			Zend_Layout::getMvcInstance()->disableLayout();
			$place->setParent($id);
		} else {
			$this->view->place = $place->getRow();
			$this->view->places = $place->getAll();
		}
	}
	
	public function getplaceAction()
	{
		Zend_Layout::getMvcInstance()->disableLayout();
		$place = new Model_Place();
		$this->view->places = $place->autocomplete($_POST['query']);
	}
}